﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using pcsse.App_Code.entity;

namespace pcsse.BBS
{
    public partial class Post : System.Web.UI.Page
    {
        string subject_id,title_name,subject_name;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ExpandForumPaths);
                if (Convert.ToInt32(Session["Priority"]) >= 3)
                {
                    ListView1.Visible = false;
                    ListView2.Visible = true;
                }
                if (Request["ID"] != null)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "select TITLE,SUBJECT_ID from T_POST where ID=@ID";
                    cmd.Parameters.Add("@ID", Request["ID"].ToString());
                    DataTable dt = db_base.getDBhandle().GetDataTableByCmd(cmd);
                    if (dt.Rows.Count == 1)
                    {
                        title_name = dt.Rows[0][0].ToString();
                        SubjectLabel.Text = title_name;
                        subject_id = dt.Rows[0][1].ToString();
                    }
                    SqlCommand cmd2 = new SqlCommand();
                    cmd2.CommandText = "select SUBJECT from T_SUBJECT where ID='" + subject_id + "'";
                    DataTable dt2 = db_base.getDBhandle().GetDataTableByCmd(cmd2);
                    if (dt2.Rows.Count == 1)
                    {
                        subject_name = dt2.Rows[0][0].ToString();
                    }
                }
            }
            else
                Response.Redirect("..\\");

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime time = DateTime.Now;
            db_base db = db_base.getDBhandle();
            person_info pi = new person_info();
            string student_id = pi.GetNumberByID(Convert.ToInt32(Session["ID"]));
            string sqlstr = "INSERT INTO T_REPLY(TITLE_ID,TEXT,STUDENT_ID,TIME,IP) VALUES(@TITLE_ID,@TEXT,@STUDENT_ID,@TIME,@IP)";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlstr;
            cmd.Parameters.Add("@TITLE_ID", Request["ID"].ToString());
            cmd.Parameters.Add("@TEXT", textcontent2.Value);
            cmd.Parameters.Add("@STUDENT_ID", student_id);
            cmd.Parameters.Add("@TIME", time.ToString());
            cmd.Parameters.Add("@IP", Request.UserHostAddress);
            db.ExecuteCmd(cmd);
            Response.Redirect(Request.Url.ToString());
        }

        private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.ExpandForumPaths);
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tempNode = currentNode;
            string curPageName = e.Context.Request.AppRelativeCurrentExecutionFilePath.Substring(2).ToUpper();
            switch (curPageName)
            {
                case "BBS/POST.ASPX":
                    tempNode.Title = title_name;
                    if (null != (tempNode = tempNode.ParentNode))
                    {
                        tempNode.Url = "ShowPost.aspx?ID=" + subject_id;
                        tempNode.Title = subject_name;
                    }
                    break;
                case "BBS/SHOWPOST.ASPX":
                    tempNode.Title = subject_name;
                    break;
            }
            return currentNode;
        }
    }
}
